In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

import dash
from dash import dcc
from dash import html
import plotly.graph_objects as go
import plotly.express as px

from dash.dependencies import Input, Output

import io
import base64
In [6]:
pip install dash plotly pandas
Requirement already satisfied: dash in c:\users\gawan\anaconda3\lib\site-packages (2.15.0)Note: you may need to restart the kernel to use updated packages.

Requirement already satisfied: plotly in c:\users\gawan\anaconda3\lib\site-packages (5.9.0)
Requirement already satisfied: pandas in c:\users\gawan\anaconda3\lib\site-packages (2.0.3)
Requirement already satisfied: Flask<3.1,>=1.0.4 in c:\users\gawan\anaconda3\lib\site-packages (from dash) (2.2.2)
Requirement already satisfied: Werkzeug<3.1 in c:\users\gawan\anaconda3\lib\site-packages (from dash) (2.2.3)
Requirement already satisfied: dash-html-components==2.0.0 in c:\users\gawan\anaconda3\lib\site-packages (from dash) (2.0.0)
Requirement already satisfied: dash-core-components==2.0.0 in c:\users\gawan\anaconda3\lib\site-packages (from dash) (2.0.0)
Requirement already satisfied: dash-table==5.0.0 in c:\users\gawan\anaconda3\lib\site-packages (from dash) (5.0.0)
Requirement already satisfied: typing-extensions>=4.1.1 in c:\users\gawan\anaconda3\lib\site-packages (from dash) (4.7.1)
Requirement already satisfied: requests in c:\users\gawan\anaconda3\lib\site-packages (from dash) (2.31.0)
Requirement already satisfied: retrying in c:\users\gawan\anaconda3\lib\site-packages (from dash) (1.3.4)
Requirement already satisfied: nest-asyncio in c:\users\gawan\anaconda3\lib\site-packages (from dash) (1.5.6)
Requirement already satisfied: setuptools in c:\users\gawan\anaconda3\lib\site-packages (from dash) (68.0.0)
Requirement already satisfied: importlib-metadata in c:\users\gawan\anaconda3\lib\site-packages (from dash) (6.0.0)
Requirement already satisfied: tenacity>=6.2.0 in c:\users\gawan\anaconda3\lib\site-packages (from plotly) (8.2.2)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\gawan\anaconda3\lib\site-packages (from pandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in c:\users\gawan\anaconda3\lib\site-packages (from pandas) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in c:\users\gawan\anaconda3\lib\site-packages (from pandas) (2023.3)
Requirement already satisfied: numpy>=1.21.0 in c:\users\gawan\anaconda3\lib\site-packages (from pandas) (1.24.3)
Requirement already satisfied: Jinja2>=3.0 in c:\users\gawan\anaconda3\lib\site-packages (from Flask<3.1,>=1.0.4->dash) (3.1.2)
Requirement already satisfied: itsdangerous>=2.0 in c:\users\gawan\anaconda3\lib\site-packages (from Flask<3.1,>=1.0.4->dash) (2.0.1)
Requirement already satisfied: click>=8.0 in c:\users\gawan\anaconda3\lib\site-packages (from Flask<3.1,>=1.0.4->dash) (8.1.7)
Requirement already satisfied: six>=1.5 in c:\users\gawan\anaconda3\lib\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
Requirement already satisfied: MarkupSafe>=2.1.1 in c:\users\gawan\anaconda3\lib\site-packages (from Werkzeug<3.1->dash) (2.1.1)
Requirement already satisfied: zipp>=0.5 in c:\users\gawan\anaconda3\lib\site-packages (from importlib-metadata->dash) (3.11.0)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\gawan\anaconda3\lib\site-packages (from requests->dash) (2.0.4)
Requirement already satisfied: idna<4,>=2.5 in c:\users\gawan\anaconda3\lib\site-packages (from requests->dash) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\gawan\anaconda3\lib\site-packages (from requests->dash) (1.26.16)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\gawan\anaconda3\lib\site-packages (from requests->dash) (2023.7.22)
Requirement already satisfied: colorama in c:\users\gawan\anaconda3\lib\site-packages (from click>=8.0->Flask<3.1,>=1.0.4->dash) (0.4.6)
In [3]:
#df_yearly = pd.read_csv('E:/e drive/DKIT/Sem 2/Dessertation/weather_data/all_yearly_full1.csv')
In [4]:
#df_monthly = pd.read_csv('E:/e drive/DKIT/Sem 2/Dessertation/weather_data/df_monthly1.csv')
In [5]:
#df_season = pd.read_csv('E:/e drive/DKIT/Sem 2/Dessertation/weather_data/df_season1.csv')
In [6]:
#df_location = pd.read_csv('E:/e drive/DKIT/Sem 2/Dessertation/weather_data/station_locations.csv')
In [7]:
pd.set_option('display.max_columns', 500)
In [2]:
df_month_location = pd.read_csv('E:/e drive/DKIT/Sem 2/Dessertation/clean_data/df_month_location1.csv')
In [3]:
df_season_location = pd.read_csv('E:/e drive/DKIT/Sem 2/Dessertation/clean_data/df_season_location1.csv')
In [4]:
df_year_location = pd.read_csv('E:/e drive/DKIT/Sem 2/Dessertation/clean_data/df_year_location1.csv')
In [5]:
df_year_location.head(3)
Out[5]:
Unnamed: 0 station_id year n_rain R5mm (Days with ≥ 5mm rain) R10mm (Days with ≥ 10mm rain) R20mm (Days with ≥ 20mm rain) SDII (Average Daily Rainfall Intensity) PRCPTOT (Total Precipitation) CDD ... TN10p (Cold Nights) TN90p (Warm Nights) TX10p (Cold Days) TX90p (Warm Days) WSDI CSDI GSL Latitude Longitude Elevation
0 0 ATHENRY 2012 275 0 0 0 0.000 0.0 0.0 ... 0.0 0.0 0.0 0.0 0 0 328 53.3 -8.8 40
1 1 ATHENRY 2013 365 78 33 9 6.703 1112.7 29.1 ... 0.0 0.0 0.0 0.0 0 0 321 53.3 -8.8 40
2 2 ATHENRY 2014 365 96 35 5 6.468 1235.3 31.9 ... 0.0 0.0 0.0 0.0 0 0 337 53.3 -8.8 40

3 rows × 36 columns

In [12]:
# Define Dash app
app = dash.Dash(__name__)

# Define available x-axis variables
x_columns = ['DTR',  'FD', 'PRCPTOT',
       'R10mm', 'R20mm', 'R5mm', 'Rx1day', 'Rx5day', 'SDII', 'SU', 'TN10p',
       'TN90p', 'TNn', 'TNx', 'TR', 'TX10p', 'TX90p', 'TXn', 'TXx', 'n_maxdy',
       'n_mindy', 'n_rain']  # Add your actual column names here

# Define app layout
app.layout = html.Div([
    html.H1("Data Visualization"),

    # Dropdown for selecting x column
    html.Div([
        html.Label("Select X Column:"),
        dcc.Dropdown(
            id='x-column-dropdown',
            options=[{'label': col, 'value': col} for col in x_columns],
            value=x_columns[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting month
    html.Div([
        html.Label("Select Month:"),
        dcc.Dropdown(
            id='month-dropdown',
            options=[{'label': str(month), 'value': month} for month in df_month_location['month'].unique()],
            value=df_month_location['month'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting season
    html.Div([
        html.Label("Select Season:"),
        dcc.Dropdown(
            id='season-dropdown',
            options=[{'label': season, 'value': season} for season in df_season_location['season'].unique()],
            value=df_season_location['season'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Line graph
    dcc.Graph(id='line-plot'),
    
    # Description of selected x-column
    html.Div(id='x-column-description', style={'margin-top': '20px'})
])

# Callback to update line plot based on dropdown selections
@app.callback(
    Output('line-plot', 'figure'),
    [Input('x-column-dropdown', 'value'),
     Input('month-dropdown', 'value'),
     Input('season-dropdown', 'value')]
)
def update_line_plot(selected_x, selected_month, selected_season):
    if selected_month is None:
        # If no month is selected, use the entire df_month_location dataset
        df_filtered = df_month_location
        title = f"{selected_x} Analysis (All Months)"
    else:
        # Filter df_month_location based on selected month
        df_filtered = df_month_location[df_month_location['month'] == selected_month]
        title = f"{selected_x} Analysis (Month {selected_month})"
        
    if selected_season is not None:
        # Filter df_season_location based on selected season
        df_filtered = df_season_location[df_season_location['season'] == selected_season]
        title += f" - {selected_season} Season"
    
    # Calculate mean for selected x column grouped by year
    avg_values = df_filtered.groupby('year')[selected_x].mean().reset_index()
    
    # Create trace for line plot
    trace = go.Scatter(
        x=avg_values['year'],
        y=avg_values[selected_x],
        mode='lines+markers',
        name=selected_x
    )
    
    # Layout for line plot
    layout = go.Layout(
        title=title,
        xaxis={'title': 'Year'},
        yaxis={'title': selected_x}
    )
    
    return {'data': [trace], 'layout': layout}

# Callback to update description of selected x-column
@app.callback(
    Output('x-column-description', 'children'),
    [Input('x-column-dropdown', 'value')]
)
def update_x_column_description(selected_x):
    # Dictionary containing descriptions of x-columns
    x_column_descriptions = {
        'PRCPTOT': 'Annual total precipitation from days ≥1 mm',
        'R10mm': ' Annual count when precipitation ≥10 mm',
        'R20mm': 'Annual count when precipitation ≥20 mm',
        'R5mm': 'Annual count when precipitation ≥5 mm',
        'Rx1day':'Annual maximum 1-day precipitation',
        'Rx5day':' Annual maximum consecutive 5-day precipitation',
        'SDII':'Ratio of annual total precipitation to number of wet days (≥1 mm)',
        'SU':'Annual count when the max temperature >25 C',
        'TNn':'Min value of daily min temperature during period of interest',
        'TNx':'Max value of daily min temperature during period of interest',
        'TR':'Counting no. of times when daily min temperature > 20 C',
        'TX10p':'xyz',
        'TX90p':'xyz1',
        'TXn':' the min value of daily max temperature (TX) during he period of interest',
        'TXx':'the maximum value of daily maximum temperature',
        'n_maxdy': 'abc',
        'n_mindy':'abcd',
        'n_rain':'abcde',
        'FD':'counting the number of days when the min temperature was less than 0°C',
        'DTR':'Avg difference between the max and min temperature during period of interest'
    }
    
    # Get the description for the selected x-column
    description = x_column_descriptions.get(selected_x, "No description available")
    
    return description

# Run the app
if __name__ == '__main__':
    app.run_server(port=8051)
In [ ]:
 

perfectly working with all filter and description¶

In [ ]:
# Define Dash app
app = dash.Dash(__name__)

# Define available x-axis variables
x_columns = ['DTR',  'FD', 'PRCPTOT',
       'R10mm', 'R20mm', 'R5mm', 'Rx1day', 'Rx5day', 'SDII', 'SU', 'TN10p',
       'TN90p', 'TNn', 'TNx', 'TR', 'TX10p', 'TX90p', 'TXn', 'TXx', 'n_maxdy',
       'n_mindy', 'n_rain']  # Add your actual column names here

# Define app layout
app.layout = html.Div([
    html.H1("Data Visualization"),

    # Dropdown for selecting x column
    html.Div([
        html.Label("Select X Column:"),
        dcc.Dropdown(
            id='x-column-dropdown',
            options=[{'label': col, 'value': col} for col in x_columns],
            value=x_columns[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting month
    html.Div([
        html.Label("Select Month:"),
        dcc.Dropdown(
            id='month-dropdown',
            options=[{'label': str(month), 'value': month} for month in df_month_location['month'].unique()],
            value=df_month_location['month'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting station_id
    html.Div([
        html.Label("Select Station ID:"),
        dcc.Dropdown(
            id='station-dropdown',
            options=[{'label': station, 'value': station} for station in df_month_location['station_id'].unique()],
            value=df_month_location['station_id'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting season
    html.Div([
        html.Label("Select Season:"),
        dcc.Dropdown(
            id='season-dropdown',
            options=[{'label': season, 'value': season} for season in df_season_location['season'].unique()],
            value=df_season_location['season'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Line graph
    dcc.Graph(id='line-plot'),
    
    # Description of selected x-column
    html.Div(id='x-column-description', style={'margin-top': '20px'})
])

# Callback to update line plot based on dropdown selections

@app.callback(
    Output('line-plot', 'figure'),
    [Input('x-column-dropdown', 'value'),
     Input('month-dropdown', 'value'),
     Input('station-dropdown', 'value'),
     Input('season-dropdown', 'value')]
)
def update_line_plot(selected_x, selected_month, selected_station, selected_season):
    if selected_month is None:
        # If no month is selected, use the entire df_month_location dataset
        df_filtered = df_month_location
        title = f"{selected_x} Analysis (All Months)"
    else:
        # Filter df_month_location based on selected month
        df_filtered = df_month_location[df_month_location['month'] == selected_month]
        title = f"{selected_x} Analysis (Month {selected_month})"
    
    if selected_station is not None:
        # Filter by selected station
        df_filtered = df_filtered[df_filtered['station_id'] == selected_station]
        title += f" - Station {selected_station}"
    
    if selected_season is not None:
        # Filter df_season_location based on selected season
        df_season_filtered = df_season_location[df_season_location['season'] == selected_season]
        # Merge df_filtered with df_season_filtered on station_id and year
        df_filtered = pd.merge(df_filtered, df_season_filtered[['station_id', 'year']], on=['station_id', 'year'])
        title += f" - Season {selected_season}"
    
    # Calculate mean for selected x column grouped by year
    avg_values = df_filtered.groupby('year')[selected_x].mean().reset_index()
    
    # Create trace for line plot
    trace = go.Scatter(
        x=avg_values['year'],
        y=avg_values[selected_x],
        mode='lines+markers',
        name=selected_x
    )
    
    # Layout for line plot
    layout = go.Layout(
        title=title,
        xaxis={'title': 'Year'},
        yaxis={'title': selected_x}
    )
    
    return {'data': [trace], 'layout': layout}


# Callback to update description of selected x-column
@app.callback(
    Output('x-column-description', 'children'),
    [Input('x-column-dropdown', 'value')]
)
def update_x_column_description(selected_indices):
    # Dictionary containing descriptions of x-columns
    x_column_descriptions = {
    'PRCPTOT': 'Annual total precipitation from days ≥1 mm',
    'R10mm': ' Annual count when precipitation ≥10 mm',
    'R20mm': 'Annual count when precipitation ≥20 mm',
    'R5mm': 'Annual count when precipitation ≥5 mm',
    'Rx1day':'Annual maximum 1-day precipitation',
    'Rx5day':' Annual maximum consecutive 5-day precipitation',
    'SDII':'Ratio of annual total precipitation to number of wet days (≥1 mm)',
    'SU':'Annual count when the max temperature >25 C',
    'TNn':'Min value of daily min temperature during period of intrest',
    'TNx':'Max value of daily min temperature during period of intrest',
    'TR':'Counting no. of times when daily min temperature > 20 C',
    'TX10p':'xyz',
    'TX90p':'xyz1',
    'TXn':' the min value of daily maxtemperature (TX) during he period of interes',
    'TXx':'the maximum value of daily maximum temperature',
    'n_maxdy': 'abc',
    'n_mindy':'abcd',
    'n_rain':'abcde',
    'FD':'counting the number of days when the min emperature was less than 0°C',
    'DTR':'Avg difference between the max and min temperature during period of intrest'
    
}
    
    # Get the description for the selected x-column
    description = x_column_descriptions.get(selected_indices, "No description available")
    
    return description

# Run the app
if __name__ == '__main__':
    app.run_server(port=8501)
In [ ]:
# Visualization 
In [ ]:
PRCPTOT (Total Precipitation),SDII (Average Daily Rainfall Intensity),R5mm (Days with ≥ 5mm rain),
R10mm (Days with ≥ 10mm rain)
,R20mm (Days with ≥ 20mm rain)
,R95pTOT (Rainfall on Very Wet Days)
,R99pTOT (Rainfall on Extremely Wet Days)
,Rx1day (Wettest day)
,Rx5day (Wettest 5-Day Period)
,CDD (Consecutive Dry Days)
,CWD (Consecutive Wet Days)
,TNn (Coldest Night)
,TNx (Warmest Night)
,TN10p (Cold Nights)
,TN90p (Warm Nights)
,FD (Frost Days)
,TR (Tropical Nights)
,CSDI (Cold Spell Duration Index)
,DTR (Diurnal Temperature Range)
,TXn (Coldest Day)
,TXx (Warmest Day)
,TX10p (Cold Days)
,TX90p (Warm Days)
,ID (Icing Days)
,SU (Summer Days)
,WSDI (Warm Spell Duration Index)
In [28]:
df_month_location_line.columns
Out[28]:
Index(['DTR', 'FD', 'ID', 'PRCPTOT', 'R10mm', 'R20mm', 'R5mm', 'Rx1day',
       'Rx5day', 'SDII', 'SU', 'TN10p', 'TN90p', 'TNn', 'TNx', 'TR', 'TX10p',
       'TX90p', 'TXn', 'TXx', 'n_maxdy', 'n_mindy', 'n_rain', 'year'],
      dtype='object')
In [30]:
df_month_location.rename(columns={
    'DTR': 'DTR (Diurnal Temperature Range)',
    'FD': 'FD (Frost Days)',
    'ID': 'ID (Icing Days)',
    'PRCPTOT': 'PRCPTOT (Total Precipitation)',
    'R10mm': 'R10mm (Days with ≥ 10mm rain)',
    'R20mm': 'R20mm (Days with ≥ 20mm rain)',
    'R5mm': 'R5mm (Days with ≥ 5mm rain)',
    'Rx1day': 'Rx1day (Wettest day)',
    'Rx5day': 'Rx5day (Wettest 5-Day Period)',
    'SDII': 'SDII (Average Daily Rainfall Intensity)',
    'SU': 'SU (Summer Days)',
    'TN10p': 'TN10p (Cold Nights)',
    'TN90p': 'TN90p (Warm Nights)',
    'TNn': 'TNn (Coldest Night)',
    'TNx': 'TNx (Warmest Night)',
    'TR': 'TR (Tropical Nights)',
    'TX10p': 'TX10p (Cold Days)',
    'TX90p': 'TX90p (Warm Days)',
    'TXn': 'TXn (Coldest Day)',
    'TXx': 'TXx (Warmest Day)'
}, inplace=True)
In [31]:
#df2 = df[df.columns.difference(['B', 'D'])]
In [32]:
df_month_location.head(5)
Out[32]:
Unnamed: 0 station_id year month n_rain R5mm (Days with ≥ 5mm rain) R10mm (Days with ≥ 10mm rain) R20mm (Days with ≥ 20mm rain) SDII (Average Daily Rainfall Intensity) PRCPTOT (Total Precipitation) Rx1day (Wettest day) Rx5day (Wettest 5-Day Period) n_maxdy n_mindy FD (Frost Days) TR (Tropical Nights) SU (Summer Days) ID (Icing Days) TNn (Coldest Night) TNx (Warmest Night) TXn (Coldest Day) TXx (Warmest Day) DTR (Diurnal Temperature Range) TN10p (Cold Nights) TN90p (Warm Nights) TX10p (Cold Days) TX90p (Warm Days) Latitude Longitude Elevation
0 0 ATHENRY 2012 1 0 0.0 0.0 0.0 0.000 0.0 0.0 0.0 31.0 31.0 1.0 0.0 0.0 0.0 -2.531 9.34 5.159 12.60 4.687 0.0 0.0 0.0 0.0 53.3 -8.8 40
1 1 ATHENRY 2012 2 0 0.0 0.0 0.0 0.000 0.0 0.0 0.0 29.0 29.0 5.0 0.0 0.0 0.0 -4.158 10.31 4.137 12.56 5.075 0.0 0.0 0.0 0.0 53.3 -8.8 40
2 2 ATHENRY 2012 3 0 0.0 0.0 0.0 0.000 0.0 0.0 0.0 31.0 31.0 2.0 0.0 0.0 0.0 -0.461 9.19 7.920 19.91 7.771 0.0 0.0 0.0 0.0 53.3 -8.8 40
3 3 ATHENRY 2012 4 30 7.0 1.0 0.0 5.308 69.0 11.6 31.9 30.0 30.0 4.0 0.0 0.0 0.0 -1.466 7.74 8.560 14.47 8.490 0.0 0.0 0.0 0.0 53.3 -8.8 40
4 4 ATHENRY 2012 5 31 3.0 0.0 0.0 4.460 44.6 9.8 18.6 31.0 31.0 1.0 0.0 1.0 0.0 -0.369 12.95 9.900 25.70 8.757 0.0 0.0 0.0 0.0 53.3 -8.8 40
In [50]:
df_month_location.to_csv('E:/e drive/DKIT/Sem 2/Dessertation/clean_data/df_month_location.csv')
In [33]:
df_month_location_line = df_month_location[df_month_location.columns.difference(['Unnamed: 0', 'station_id',
                                                         'month','Latitude', 
                                                        'Longitude', 'Elevation'])]
In [14]:
df_year_location.head(3)
Out[14]:
Unnamed: 0 station_id year n_rain R5mm R10mm R20mm SDII PRCPTOT CDD CWD Rx1day Rx5day R95pTOT R99pTOT n_maxdy n_mindy FD TR SU ID TNn TNx TXn TXx DTR TN10p TN90p TX10p TX90p WSDI CSDI GSL Latitude Longitude Elevation
0 0 ATHENRY 2012 275 0.0 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 366.0 366.0 31.0 0.0 2.0 0.0 -4.158 16.39 3.054 25.70 6.879 0.0 0.0 0.0 0.0 0.0 0.0 328.0 53.3 -8.8 40
1 1 ATHENRY 2013 365 78.0 33.0 9.0 6.703 1112.7 29.1 87.3 0.0 0.0 19.0 16.0 365.0 365.0 55.0 0.0 7.0 0.0 -6.326 16.01 2.570 29.23 7.216 0.0 0.0 0.0 0.0 0.0 0.0 321.0 53.3 -8.8 40
2 2 ATHENRY 2014 365 96.0 35.0 5.0 6.468 1235.3 31.9 58.1 0.0 0.0 18.0 14.0 365.0 365.0 29.0 0.0 2.0 0.0 -4.720 16.42 4.791 26.65 7.532 0.0 0.0 0.0 0.0 0.0 0.0 337.0 53.3 -8.8 40
In [6]:
# year location line columns only
df_year_location_line = df_year_location[df_year_location.columns.difference(['Unnamed: 0', 'station_id',
                                                         'Latitude', 
                                                        'Longitude', 'Elevation'])]
In [7]:
df_year_location_line.head(3)
Out[7]:
CDD CSDI CWD DTR (Diurnal Temperature Range) FD (Frost Days) GSL ID (Icing Days) PRCPTOT (Total Precipitation) R10mm (Days with ≥ 10mm rain) R20mm (Days with ≥ 20mm rain) ... TR (Tropical Nights) TX10p (Cold Days) TX90p (Warm Days) TXn (Coldest Day) TXx (Warmest Day) WSDI n_maxdy n_mindy n_rain year
0 0.0 0 0.0 6.879 31 328 0 0.0 0 0 ... 0 0.0 0.0 3.054 25.70 0 366 366 275 2012
1 29.1 0 87.3 7.216 55 321 0 1112.7 33 9 ... 0 0.0 0.0 2.570 29.23 0 365 365 365 2013
2 31.9 0 58.1 7.532 29 337 0 1235.3 35 5 ... 0 0.0 0.0 4.791 26.65 0 365 365 365 2014

3 rows × 31 columns

In [34]:
df_month_location_line.head(3)
Out[34]:
DTR (Diurnal Temperature Range) FD (Frost Days) ID (Icing Days) PRCPTOT (Total Precipitation) R10mm (Days with ≥ 10mm rain) R20mm (Days with ≥ 20mm rain) R5mm (Days with ≥ 5mm rain) Rx1day (Wettest day) Rx5day (Wettest 5-Day Period) SDII (Average Daily Rainfall Intensity) SU (Summer Days) TN10p (Cold Nights) TN90p (Warm Nights) TNn (Coldest Night) TNx (Warmest Night) TR (Tropical Nights) TX10p (Cold Days) TX90p (Warm Days) TXn (Coldest Day) TXx (Warmest Day) n_maxdy n_mindy n_rain year
0 4.687 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -2.531 9.34 0.0 0.0 0.0 5.159 12.60 31.0 31.0 0 2012
1 5.075 5.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -4.158 10.31 0.0 0.0 0.0 4.137 12.56 29.0 29.0 0 2012
2 7.771 2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -0.461 9.19 0.0 0.0 0.0 7.920 19.91 31.0 31.0 0 2012
In [22]:
df_month_location_line.columns
Out[22]:
Index(['DTR', 'FD', 'ID', 'PRCPTOT', 'R10mm', 'R20mm', 'R5mm', 'Rx1day',
       'Rx5day', 'SDII', 'SU', 'TN10p', 'TN90p', 'TNn', 'TNx', 'TR', 'TX10p',
       'TX90p', 'TXn', 'TXx', 'n_maxdy', 'n_mindy', 'n_rain', 'year'],
      dtype='object')
In [35]:
df_month_location_line.columns[:-1]
Out[35]:
Index(['DTR (Diurnal Temperature Range)', 'FD (Frost Days)', 'ID (Icing Days)',
       'PRCPTOT (Total Precipitation)', 'R10mm (Days with ≥ 10mm rain)',
       'R20mm (Days with ≥ 20mm rain)', 'R5mm (Days with ≥ 5mm rain)',
       'Rx1day (Wettest day)', 'Rx5day (Wettest 5-Day Period)',
       'SDII (Average Daily Rainfall Intensity)', 'SU (Summer Days)',
       'TN10p (Cold Nights)', 'TN90p (Warm Nights)', 'TNn (Coldest Night)',
       'TNx (Warmest Night)', 'TR (Tropical Nights)', 'TX10p (Cold Days)',
       'TX90p (Warm Days)', 'TXn (Coldest Day)', 'TXx (Warmest Day)',
       'n_maxdy', 'n_mindy', 'n_rain'],
      dtype='object')
In [52]:
#fig, ax = plt.subplots(figsize=(10, 6))  # Adjust figure size as needed

for column in df_month_location_line.columns[:-1]: 
    plt.figure()  # Create a new figure for each line plot
    sns.lineplot(x='year', y=column, data=df_month_location_line, label=column)
    plt.xlabel('Year')
    plt.ylabel(column)
    plt.title(f'Line Graph - {column}')
    plt.legend()

# Display the plots
plt.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[52], line 5
      3 for column in df_month_location_line.columns[:-1]: 
      4     plt.figure()  # Create a new figure for each line plot
----> 5     sns.regplot(x='year', y=column, data=df_month_location_line, label=column, ci=none)
      6     plt.xlabel('Year')
      7     plt.ylabel(column)

NameError: name 'none' is not defined
<Figure size 640x480 with 0 Axes>
In [55]:
# below plot working with regression line
In [18]:
df_year_location
Out[18]:
Unnamed: 0 station_id year n_rain R5mm R10mm R20mm SDII PRCPTOT CDD CWD Rx1day Rx5day R95pTOT R99pTOT n_maxdy n_mindy FD TR SU ID TNn TNx TXn TXx DTR TN10p TN90p TX10p TX90p WSDI CSDI GSL Latitude Longitude Elevation
0 0 ATHENRY 2012 275 0.0 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 366.0 366.0 31.0 0.0 2.0 0.0 -4.158 16.39 3.054 25.70 6.879 0.000 0.000 0.000 0.000 0.0 0.0 328.0 53.3 -8.8 40
1 1 ATHENRY 2013 365 78.0 33.0 9.0 6.703 1112.7 29.1 87.3 0.0 0.0 19.0 16.0 365.0 365.0 55.0 0.0 7.0 0.0 -6.326 16.01 2.570 29.23 7.216 0.000 0.000 0.000 0.000 0.0 0.0 321.0 53.3 -8.8 40
2 2 ATHENRY 2014 365 96.0 35.0 5.0 6.468 1235.3 31.9 58.1 0.0 0.0 18.0 14.0 365.0 365.0 29.0 0.0 2.0 0.0 -4.720 16.42 4.791 26.65 7.532 0.000 0.000 0.000 0.000 0.0 0.0 337.0 53.3 -8.8 40
3 3 ATHENRY 2015 365 116.0 50.0 10.0 7.527 1543.1 34.5 85.3 0.0 0.0 13.0 24.0 365.0 365.0 29.0 0.0 0.0 0.0 -5.380 15.35 2.837 22.83 7.469 0.000 0.000 0.000 0.000 0.0 0.0 353.0 53.3 -8.8 40
4 4 ATHENRY 2016 366 76.0 23.0 3.0 5.623 1045.8 30.0 59.2 0.0 0.0 17.0 17.0 366.0 366.0 45.0 0.0 2.0 0.0 -5.104 16.99 3.212 28.41 7.238 0.000 0.000 0.000 0.000 0.0 0.0 304.0 53.3 -8.8 40
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1736 1736 VALENTIA OBSERVATORY 2019 365 117.0 59.0 11.0 8.039 1664.0 37.0 84.6 278.5 73.4 14.0 21.0 365.0 365.0 5.0 0.0 1.0 0.0 -1.339 17.97 4.809 28.03 5.048 5.479 24.110 4.384 9.041 8.0 0.0 359.0 51.9 -10.2 24
1737 1737 VALENTIA OBSERVATORY 2020 366 124.0 64.0 20.0 9.283 1912.3 60.6 108.9 555.3 191.4 10.0 26.0 366.0 366.0 1.0 0.0 0.0 0.0 -1.244 17.02 6.532 24.07 5.254 4.918 14.208 2.732 12.568 12.0 0.0 360.0 51.9 -10.2 24
1738 1738 VALENTIA OBSERVATORY 2021 365 101.0 47.0 17.0 7.700 1524.6 61.4 95.8 266.6 100.8 17.0 20.0 365.0 365.0 10.0 2.0 6.0 0.0 -2.126 20.49 3.781 28.30 5.302 5.753 20.822 6.027 13.973 6.0 0.0 349.0 51.9 -10.2 24
1739 1739 VALENTIA OBSERVATORY 2022 365 109.0 60.0 19.0 8.786 1651.7 37.3 108.4 430.9 37.3 15.0 18.0 364.0 364.0 14.0 0.0 2.0 0.0 -4.480 17.07 3.331 25.53 5.452 4.670 24.725 4.121 18.956 22.0 10.0 340.0 51.9 -10.2 24
1740 1740 VALENTIA OBSERVATORY 2023 365 124.0 68.0 20.0 8.978 1912.4 67.8 95.2 559.5 150.2 20.0 18.0 365.0 365.0 5.0 0.0 3.0 0.0 -1.890 19.17 5.448 27.59 4.898 1.096 30.959 2.192 18.082 20.0 0.0 359.0 51.9 -10.2 24

1741 rows × 36 columns

In [24]:
df_year_location[df_year_location['year'] == 2020]
Out[24]:
Unnamed: 0 station_id year n_rain R5mm R10mm R20mm SDII PRCPTOT CDD CWD Rx1day Rx5day R95pTOT R99pTOT n_maxdy n_mindy FD TR SU ID TNn TNx TXn TXx DTR TN10p TN90p TX10p TX90p WSDI CSDI GSL Latitude Longitude Elevation
8 8 ATHENRY 2020 366 104.0 47.0 12.0 7.776 1446.4 35.3 96.7 0.0 0.0 15.0 23.0 366.0 366.0 31.0 0.0 2.0 0.0 -4.517 16.07 2.940 26.64 7.299 0.000 0.000 0.000 0.000 0.0 0.0 337.0 53.3 -8.8 40
39 39 BALLYHAISE 2020 366 89.0 28.0 5.0 6.449 1122.1 32.1 64.4 0.0 0.0 16.0 25.0 366.0 366.0 26.0 0.0 0.0 1.0 -3.904 15.52 -0.353 24.48 6.980 0.000 0.000 0.000 0.000 0.0 0.0 336.0 54.1 -7.3 78
107 107 BELMULLET 2020 366 112.0 46.0 5.0 6.715 1490.8 36.2 76.1 321.3 101.8 16.0 28.0 366.0 366.0 4.0 0.0 1.0 0.0 -1.670 16.21 5.817 25.59 5.212 3.825 14.208 3.825 14.208 14.0 0.0 360.0 54.2 -10.0 9
233 233 CASEMENT 2020 366 49.0 24.0 3.0 5.417 785.5 35.6 50.5 164.0 35.6 16.0 12.0 366.0 366.0 26.0 0.0 2.0 0.0 -4.300 16.70 0.000 25.70 7.119 6.284 12.568 7.650 17.486 10.0 0.0 317.0 53.3 -6.4 91
314 314 CLAREMORRIS 2020 366 115.0 63.0 11.0 8.032 1606.4 46.4 83.2 365.8 172.5 22.0 19.0 366.0 366.0 27.0 0.0 3.0 0.0 -3.867 16.07 3.289 26.35 7.286 4.645 9.836 3.825 16.120 24.0 0.0 360.0 53.7 -9.0 68
439 439 CORK AIRPORT 2020 366 92.0 43.0 15.0 8.104 1369.5 43.0 69.4 319.7 43.0 20.0 13.0 366.0 366.0 6.0 0.0 0.0 0.0 -0.900 17.00 3.100 24.10 6.185 3.279 13.388 3.279 15.574 6.0 0.0 337.0 51.8 -8.5 155
522 522 DUBLIN AIRPORT 2020 366 46.0 20.0 1.0 5.168 713.2 24.7 43.5 97.3 0.0 15.0 9.0 366.0 366.0 31.0 0.0 0.0 0.0 -4.400 16.40 1.000 25.00 7.327 14.481 9.016 6.011 17.760 17.0 0.0 317.0 53.4 -6.2 71
605 605 DUNSANY 2020 366 60.0 29.0 4.0 5.725 893.1 29.2 56.4 132.6 29.2 19.0 9.0 366.0 366.0 30.0 0.0 0.0 1.0 -4.828 15.88 -0.865 24.95 7.175 6.011 15.301 4.918 18.852 10.0 0.0 336.0 53.5 -6.7 83
617 617 FINNER 2020 366 100.0 53.0 11.0 7.442 1458.7 41.4 78.4 0.0 0.0 17.0 25.0 366.0 366.0 8.0 0.0 1.0 0.0 -2.645 16.47 2.142 25.93 6.043 0.000 0.000 0.000 0.000 0.0 0.0 360.0 54.5 -8.2 33
631 631 GURTEEN 2020 336 0.0 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 366.0 366.0 25.0 0.0 1.0 0.0 -3.991 16.99 3.155 25.85 7.374 0.000 0.000 0.000 0.000 0.0 0.0 337.0 53.0 -8.0 75
714 714 JOHNSTOWN 2020 366 76.0 39.0 9.0 7.548 1117.1 31.2 54.1 222.8 0.0 18.0 12.0 366.0 366.0 4.0 0.0 0.0 0.0 -2.430 16.73 4.149 22.67 5.605 2.732 20.492 4.918 12.022 8.0 0.0 337.0 52.3 -6.5 62
802 802 KNOCK AIRPORT 2020 366 112.0 49.0 4.0 7.091 1475.0 62.8 93.7 0.0 0.0 21.0 25.0 366.0 366.0 20.0 0.0 0.0 0.0 -2.100 15.30 1.300 24.30 6.142 0.000 0.000 0.000 0.000 0.0 0.0 272.0 53.9 -8.8 201
814 814 MACE HEAD 2020 366 81.0 33.0 11.0 6.620 1165.2 32.4 74.2 0.0 0.0 19.0 23.0 366.0 366.0 0.0 0.0 0.0 0.0 0.217 17.18 5.744 24.47 4.564 0.000 0.000 0.000 0.000 0.0 0.0 360.0 53.3 -9.9 21
881 881 MALIN HEAD 2020 366 86.0 36.0 4.0 6.105 1300.4 33.4 65.7 280.3 61.6 17.0 18.0 365.0 365.0 0.0 0.0 0.0 0.0 0.510 16.74 5.010 23.17 4.702 3.562 13.425 3.014 13.151 0.0 0.0 360.0 55.4 -7.3 20
964 964 MARKREE 2020 366 100.0 42.0 9.0 6.881 1376.3 52.4 80.3 343.0 131.5 22.0 25.0 366.0 366.0 38.0 0.0 5.0 0.0 -5.322 15.69 1.362 25.61 7.276 4.645 16.120 6.831 12.842 6.0 0.0 359.0 54.2 -8.5 34
1027 1027 MOORE PARK 2020 366 74.0 32.0 6.0 6.262 1070.8 33.5 60.0 163.0 33.5 18.0 23.0 366.0 366.0 34.0 0.0 0.0 0.0 -5.203 17.93 2.426 24.41 7.627 3.825 13.115 3.279 15.574 7.0 0.0 356.0 52.2 -8.3 46
1099 1099 MT DILLON 2020 366 88.0 35.0 8.0 6.742 1227.0 34.7 71.3 354.4 64.9 12.0 19.0 366.0 366.0 46.0 0.0 6.0 0.0 -5.459 15.68 1.417 26.77 8.417 0.000 0.000 0.000 0.000 0.0 0.0 336.0 53.7 -8.0 39
1180 1180 MULLINGAR 2020 366 70.0 27.0 3.0 6.077 1045.3 32.1 65.9 259.3 59.7 19.0 19.0 366.0 366.0 36.0 0.0 2.0 0.0 -6.138 16.14 0.363 25.65 7.512 8.197 14.754 4.372 19.399 9.0 0.0 355.0 53.5 -7.4 101
1243 1243 NEWPORT 2020 366 138.0 70.0 19.0 9.069 2031.5 55.8 108.8 570.4 211.1 16.0 26.0 366.0 366.0 4.0 0.0 4.0 0.0 -1.429 15.83 4.982 27.11 6.308 4.918 12.295 3.005 17.213 13.0 0.0 360.0 53.9 -9.6 22
1300 1300 OAK PARK 2020 366 65.0 30.0 2.0 6.125 869.8 31.2 66.5 128.3 31.2 19.0 9.0 366.0 366.0 26.0 0.0 4.0 0.0 -4.511 16.27 1.285 26.19 7.557 4.372 14.481 4.645 18.306 14.0 0.0 337.0 52.9 -6.9 62
1383 1383 PHOENIX PARK 2020 366 48.0 21.0 1.0 5.068 724.7 33.1 51.6 102.1 33.1 18.0 6.0 366.0 366.0 21.0 0.0 3.0 0.0 -3.196 17.25 0.186 25.99 7.411 3.552 18.033 6.557 19.399 17.0 0.0 337.0 53.4 -6.3 48
1466 1466 ROCHES POINT 2020 366 75.0 34.0 7.0 6.879 1066.3 45.5 70.5 179.4 45.5 20.0 11.0 366.0 366.0 0.0 0.0 0.0 0.0 0.547 17.39 4.819 23.53 5.055 3.279 15.027 3.825 13.115 0.0 0.0 360.0 51.8 -8.2 40
1602 1602 SHANNON AIRPORT 2020 366 85.0 35.0 7.0 6.524 1226.5 34.8 84.4 398.2 124.1 17.0 17.0 366.0 366.0 11.0 0.0 2.0 0.0 -2.300 17.00 3.900 26.00 6.579 6.011 10.383 6.011 16.393 7.0 0.0 360.0 52.7 -8.9 15
1654 1654 SherkinIsland 2020 366 90.0 39.0 8.0 7.078 1238.7 36.2 72.7 222.0 68.2 15.0 13.0 366.0 366.0 0.0 0.0 0.0 0.0 1.035 17.03 6.394 22.29 4.388 0.000 0.000 0.000 0.000 0.0 0.0 360.0 51.5 -9.4 21
1737 1737 VALENTIA OBSERVATORY 2020 366 124.0 64.0 20.0 9.283 1912.3 60.6 108.9 555.3 191.4 10.0 26.0 366.0 366.0 1.0 0.0 0.0 0.0 -1.244 17.02 6.532 24.07 5.254 4.918 14.208 2.732 12.568 12.0 0.0 360.0 51.9 -10.2 24
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [8]:
# Iterate through each column except the last one
for column in df_year_location_line.columns[:-1]: 
    plt.figure()  # Create a new figure for each line plot
    sns.lineplot(x='year', y=column, data=df_year_location_line, label=column)
    
    # Add regression line
    sns.regplot(x='year', y=column, data=df_year_location_line, scatter=False, color='red', label='Regression line')
    
    plt.xlabel('Year')
    plt.ylabel(column)
    plt.title(f'Line Graph - {column}')
    plt.legend()

# Display the plots
plt.show() 
C:\Users\gawan\AppData\Local\Temp\ipykernel_17236\2042612340.py:3: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`). Consider using `matplotlib.pyplot.close()`.
  plt.figure()  # Create a new figure for each line plot
In [53]:
import matplotlib.pyplot as plt
import seaborn as sns

# Iterate through each column except the last one
for column in df_month_location_line.columns[:-1]: 
    plt.figure()  # Create a new figure for each line plot
    sns.lineplot(x='year', y=column, data=df_month_location_line, label=column)
    
    # Add regression line
    sns.regplot(x='year', y=column, data=df_month_location_line, scatter=False, color='red', label='Regression line')
    
    plt.xlabel('Year')
    plt.ylabel(column)
    plt.title(f'Line Graph - {column}')
    plt.legend()

# Display the plots
plt.show() 
C:\Users\gawan\AppData\Local\Temp\ipykernel_20080\1922432552.py:6: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`). Consider using `matplotlib.pyplot.close()`.
  plt.figure()  # Create a new figure for each line plot
In [ ]:
 
In [ ]:
 
In [54]:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Iterate through each column except the last one
for column in df_month_location_line.columns[:-1]: 
    plt.figure()  # Create a new figure for each line plot
    sns.lineplot(x='year', y=column, data=df_month_location_line, label=column)
    
    # Calculate regression line
    x = df_month_location_line['year']
    y = df_month_location_line[column]
    slope, intercept = np.polyfit(x, y, 1)
    plt.plot(x, slope*x + intercept, color='red', label='Regression line')
    
    plt.xlabel('Year')
    plt.ylabel(column)
    plt.title(f'Line Graph - {column}')
    plt.legend()

# Display the plots
plt.show() 
C:\Users\gawan\AppData\Local\Temp\ipykernel_20080\930096363.py:7: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`). Consider using `matplotlib.pyplot.close()`.
  plt.figure()  # Create a new figure for each line plot
In [ ]:
 
In [ ]:
 

changing name of df_season_location column name¶

In [37]:
df_season_location.columns
Out[37]:
Index(['Unnamed: 0', 'station_id', 'year', 'season', 'n_rain', 'R5mm', 'R10mm',
       'R20mm', 'SDII', 'PRCPTOT', 'Rx1day', 'Rx5day', 'n_maxdy', 'n_mindy',
       'FD', 'TR', 'SU', 'ID', 'TNn', 'TNx', 'TXn', 'TXx', 'DTR', 'TN10p',
       'TN90p', 'TX10p', 'TX90p', 'Latitude', 'Longitude', 'Elevation'],
      dtype='object')
In [38]:
df_season_location.rename(columns={
    'DTR': 'DTR (Diurnal Temperature Range)',
    'FD': 'FD (Frost Days)',
    'ID': 'ID (Icing Days)',
    'PRCPTOT': 'PRCPTOT (Total Precipitation)',
    'R10mm': 'R10mm (Days with ≥ 10mm rain)',
    'R20mm': 'R20mm (Days with ≥ 20mm rain)',
    'R5mm': 'R5mm (Days with ≥ 5mm rain)',
    'Rx1day': 'Rx1day (Wettest day)',
    'Rx5day': 'Rx5day (Wettest 5-Day Period)',
    'SDII': 'SDII (Average Daily Rainfall Intensity)',
    'SU': 'SU (Summer Days)',
    'TN10p': 'TN10p (Cold Nights)',
    'TN90p': 'TN90p (Warm Nights)',
    'TNn': 'TNn (Coldest Night)',
    'TNx': 'TNx (Warmest Night)',
    'TR': 'TR (Tropical Nights)',
    'TX10p': 'TX10p (Cold Days)',
    'TX90p': 'TX90p (Warm Days)',
    'TXn': 'TXn (Coldest Day)',
    'TXx': 'TXx (Warmest Day)'
}, inplace=True)
In [49]:
df_season_location.to_csv('E:/e drive/DKIT/Sem 2/Dessertation/clean_data/df_season_location.csv')

changing_column name for df_year_location¶

In [41]:
df_year_location.columns
Out[41]:
Index(['Unnamed: 0', 'station_id', 'year', 'n_rain', 'R5mm', 'R10mm', 'R20mm',
       'SDII', 'PRCPTOT', 'CDD', 'CWD', 'Rx1day', 'Rx5day', 'R95pTOT',
       'R99pTOT', 'n_maxdy', 'n_mindy', 'FD', 'TR', 'SU', 'ID', 'TNn', 'TNx',
       'TXn', 'TXx', 'DTR', 'TN10p', 'TN90p', 'TX10p', 'TX90p', 'WSDI', 'CSDI',
       'GSL', 'Latitude', 'Longitude', 'Elevation'],
      dtype='object')
In [44]:
df_year_location.rename(columns={
    'DTR': 'DTR (Diurnal Temperature Range)',
    'FD': 'FD (Frost Days)',
    'ID': 'ID (Icing Days)',
    'PRCPTOT': 'PRCPTOT (Total Precipitation)',
    'R10mm': 'R10mm (Days with ≥ 10mm rain)',
    'R20mm': 'R20mm (Days with ≥ 20mm rain)',
    'R5mm': 'R5mm (Days with ≥ 5mm rain)',
    'Rx1day': 'Rx1day (Wettest day)',
    'Rx5day': 'Rx5day (Wettest 5-Day Period)',
    'SDII': 'SDII (Average Daily Rainfall Intensity)',
    'SU': 'SU (Summer Days)',
    'TN10p': 'TN10p (Cold Nights)',
    'TN90p': 'TN90p (Warm Nights)',
    'TNn': 'TNn (Coldest Night)',
    'TNx': 'TNx (Warmest Night)',
    'TR': 'TR (Tropical Nights)',
    'TX10p': 'TX10p (Cold Days)',
    'TX90p': 'TX90p (Warm Days)',
    'TXn': 'TXn (Coldest Day)',
    'TXx': 'TXx (Warmest Day)'
}, inplace=True)
In [46]:
df_year_location.head(3)
Out[46]:
Unnamed: 0 station_id year n_rain R5mm (Days with ≥ 5mm rain) R10mm (Days with ≥ 10mm rain) R20mm (Days with ≥ 20mm rain) SDII (Average Daily Rainfall Intensity) PRCPTOT (Total Precipitation) CDD CWD Rx1day (Wettest day) Rx5day (Wettest 5-Day Period) R95pTOT R99pTOT n_maxdy n_mindy FD (Frost Days) TR (Tropical Nights) SU (Summer Days) ID (Icing Days) TNn (Coldest Night) TNx (Warmest Night) TXn (Coldest Day) TXx (Warmest Day) DTR (Diurnal Temperature Range) TN10p (Cold Nights) TN90p (Warm Nights) TX10p (Cold Days) TX90p (Warm Days) WSDI CSDI GSL Latitude Longitude Elevation
0 0 ATHENRY 2012 275 0.0 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 366.0 366.0 31.0 0.0 2.0 0.0 -4.158 16.39 3.054 25.70 6.879 0.0 0.0 0.0 0.0 0.0 0.0 328.0 53.3 -8.8 40
1 1 ATHENRY 2013 365 78.0 33.0 9.0 6.703 1112.7 29.1 87.3 0.0 0.0 19.0 16.0 365.0 365.0 55.0 0.0 7.0 0.0 -6.326 16.01 2.570 29.23 7.216 0.0 0.0 0.0 0.0 0.0 0.0 321.0 53.3 -8.8 40
2 2 ATHENRY 2014 365 96.0 35.0 5.0 6.468 1235.3 31.9 58.1 0.0 0.0 18.0 14.0 365.0 365.0 29.0 0.0 2.0 0.0 -4.720 16.42 4.791 26.65 7.532 0.0 0.0 0.0 0.0 0.0 0.0 337.0 53.3 -8.8 40
In [47]:
df_year_location.to_csv('E:/e drive/DKIT/Sem 2/Dessertation/clean_data/df_year_location.csv')
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
# Define Dash app
app = dash.Dash(__name__)

# Define available x-axis variables
x_columns = ['DTR',  'FD', 'PRCPTOT',
       'R10mm', 'R20mm', 'R5mm', 'Rx1day', 'Rx5day', 'SDII', 'SU', 'TN10p',
       'TN90p', 'TNn', 'TNx', 'TR', 'TX10p', 'TX90p', 'TXn', 'TXx', 'n_maxdy',
       'n_mindy', 'n_rain']  # Add your actual column names here

# Define app layout
app.layout = html.Div([
    html.H1("Data Visualization"),

    # Dropdown for selecting x column
    html.Div([
        html.Label("Select X Column:"),
        dcc.Dropdown(
            id='x-column-dropdown',
            options=[{'label': col, 'value': col} for col in x_columns],
            value=x_columns[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting month
    html.Div([
        html.Label("Select Month:"),
        dcc.Dropdown(
            id='month-dropdown',
            options=[{'label': str(month), 'value': month} for month in df_month_location['month'].unique()],
            value=df_month_location['month'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting station
    html.Div([
        html.Label("Select Station:"),
        dcc.Dropdown(
            id='station-dropdown',
            options=[{'label': str(station), 'value': station} for station in df_month_location['station_id'].unique()],
            value=df_month_location['station_id'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting season
    html.Div([
        html.Label("Select Season:"),
        dcc.Dropdown(
            id='season-dropdown',
            options=[{'label': season, 'value': season} for season in df_season_location['season'].unique()],
            value=df_season_location['season'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Line graph
    dcc.Graph(id='line-plot'),
    
    # Description of selected x-column
    html.Div(id='x-column-description', style={'margin-top': '20px'})
])

# Callback to update line plot based on dropdown selections
@app.callback(
    Output('line-plot', 'figure'),
    [Input('x-column-dropdown', 'value'),
     Input('month-dropdown', 'value'),
     Input('station-dropdown', 'value'),
     Input('season-dropdown', 'value')]
)
def update_line_plot(selected_x, selected_month, selected_station, selected_season):
    if selected_month is None:
        # If no month is selected, use the entire df_month_location dataset
        df_filtered = df_month_location
        title = f"{selected_x} Analysis (All Months)"
    else:
        # Filter df_month_location based on selected month
        df_filtered = df_month_location[df_month_location['month'] == selected_month]
        title = f"{selected_x} Analysis (Month {selected_month})"
    
    if selected_station is not None:
        # Filter by selected station
        df_filtered = df_filtered[df_filtered['station_id'] == selected_station]
        title += f" - Station {selected_station}"
    
    if selected_season is not None:
        # Filter df_season_location based on selected season
        df_season_filtered = df_season_location[df_season_location['season'] == selected_season]
        # Merge df_filtered with df_season_filtered on station_id and year
        df_filtered = pd.merge(df_filtered, df_season_filtered[['station_id', 'year']], on=['station_id', 'year'])
        title += f" - Season {selected_season}"
    
    # Calculate mean for selected x column grouped by year
    avg_values = df_filtered.groupby('year')[selected_x].mean().reset_index()
    
    # Create trace for line plot
    trace = go.Scatter(
        x=avg_values['year'],
        y=avg_values[selected_x],
        mode='lines+markers',
        name=selected_x
    )
    
    # Layout for line plot
    layout = go.Layout(
        title=title,
        xaxis={'title': 'Year'},
        yaxis={'title': selected_x}
    )
    
    return {'data': [trace], 'layout': layout}

@app.callback(
    Output('x-column-description', 'children'),
    [Input('x-column-dropdown', 'value')]
)
def update_x_column_description(selected_x):
    print("Selected x-column:", selected_x)
    # Dictionary containing descriptions of x-columns
    x_column_descriptions = {
        'PRCPTOT': 'Annual total precipitation from days ≥1 mm',
        'R10mm': ' Annual count when precipitation ≥10 mm',
        'R20mm': 'Annual count when precipitation ≥20 mm',
        'R5mm': 'Annual count when precipitation ≥5 mm',
        'Rx1day':'Annual maximum 1-day precipitation',
        'Rx5day':' Annual maximum consecutive 5-day precipitation',
        'SDII':'Ratio of annual total precipitation to number of wet days (≥1 mm)',
        'SU':'Annual count when the max temperature >25 C',
        'TNn':'Min value of daily min temperature during period of interest',
        'TNx':'Max value of daily min temperature during period of interest',
        'TR':'Counting no. of times when daily min temperature > 20 C',
        'TX10p':'xyz',
        'TX90p':'xyz1',
        'TXn':' the min value of daily max temperature (TX) during the period of interest',
        'TXx':'the maximum value of daily maximum temperature',
        'n_maxdy': 'abc',
        'n_mindy':'abcd',
        'n_rain':'abcde',
        'FD':'counting the number of days when the min temperature was less than 0°C',
        'DTR':'Avg difference between the max and min temperature during the period of interest'
    }
    
    if selected_x is None:
        print("No x-column selected")
        return "Select one column"
    
    # Get the description for the selected x-column
    description = x_column_descriptions.get(selected_x, "No description available")
    print("Description:", description)
    
    return description


# Run the app
if __name__ == '__main__':
    app.run_server(port=8503)
In [ ]:
 
In [ ]:
 
In [ ]:
# Define Dash app
app = dash.Dash(__name__)

# Define available x-axis variables
x_columns = ['DTR', 'FD', 'PRCPTOT', 'R10mm', 'R20mm', 'R5mm', 'Rx1day', 'Rx5day', 'SDII', 'SU', 'TN10p',
             'TN90p', 'TNn', 'TNx', 'TR', 'TX10p', 'TX90p', 'TXn', 'TXx', 'n_maxdy', 'n_mindy', 'n_rain']

# Define app layout
app.layout = html.Div([
    html.H1("Data Visualization"),

    # Dropdown for selecting x column
    html.Div([
        html.Label("Select X Column:"),
        dcc.Dropdown(
            id='x-column-dropdown',
            options=[{'label': col, 'value': col} for col in x_columns],
            value=x_columns[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Dropdown for selecting month
    html.Div([
        html.Label("Select Month:"),
        dcc.Dropdown(
            id='month-dropdown',
            options=[{'label': str(month), 'value': month} for month in df_month_location['month'].unique()],
            value=df_month_location['month'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Dropdown for selecting station
    html.Div([
        html.Label("Select Station:"),
        dcc.Dropdown(
            id='station-dropdown',
            options=[{'label': str(station), 'value': station} for station in df_month_location['station_id'].unique()],
            value=df_month_location['station_id'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Dropdown for selecting season
    html.Div([
        html.Label("Select Season:"),
        dcc.Dropdown(
            id='season-dropdown',
            options=[{'label': season, 'value': season} for season in df_season_location['season'].unique()],
            value=df_season_location['season'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Line graph
    dcc.Graph(id='line-plot'),

    # Description of selected x-column
    html.Div(id='x-column-description', style={'margin-top': '20px'})
])


# Callback to update line plot based on dropdown selections
@app.callback(
    Output('line-plot', 'figure'),
    [Input('x-column-dropdown', 'value'),
     Input('month-dropdown', 'value'),
     Input('station-dropdown', 'value'),
     Input('season-dropdown', 'value')]
)
def update_line_plot(selected_x, selected_month, selected_station, selected_season):
    if selected_month is None:
        # If no month is selected, use the entire df_month_location dataset
        df_filtered = df_month_location
        title = f"{selected_x} Analysis (All Months)"
    else:
        # Filter df_month_location based on selected month
        df_filtered = df_month_location[df_month_location['month'] == selected_month]
        title = f"{selected_x} Analysis (Month {selected_month})"

    if selected_station is not None:
        # Filter by selected station
        df_filtered = df_filtered[df_filtered['station_id'] == selected_station]
        title += f" - Station {selected_station}"

    if selected_season is not None:
        # Filter df_season_location based on selected season
        df_season_filtered = df_season_location[df_season_location['season'] == selected_season]
        # Merge df_filtered with df_season_filtered on station_id and year
        df_filtered = pd.merge(df_filtered, df_season_filtered[['station_id', 'year']], on=['station_id', 'year'])
        title += f" - Season {selected_season}"

    # Calculate mean for selected x column grouped by year
    avg_values = df_filtered.groupby('year')[selected_x].mean().reset_index()

    # Create trace for line plot
    trace = go.Scatter(
        x=avg_values['year'],
        y=avg_values[selected_x],
        mode='lines+markers',
        name=selected_x
    )

    # Layout for line plot
    layout = go.Layout(
        title=title,
        xaxis={'title': 'Year'},
        yaxis={'title': selected_x}
    )

    return {'data': [trace], 'layout': layout}


# Callback to update description of selected x-column
@app.callback(
    Output('x-column-description', 'children'),
    [Input('x-column-dropdown', 'value')]
)
def update_x_column_description(selected_x):
    # Dictionary containing descriptions of x-columns
    x_column_descriptions = {
        'PRCPTOT': 'Annual total precipitation from days ≥1 mm',
        'R10mm': ' Annual count when precipitation ≥10 mm',
        'R20mm': 'Annual count when precipitation ≥20 mm',
        'R5mm': 'Annual count when precipitation ≥5 mm',
        'Rx1day': 'Annual maximum 1-day precipitation',
        'Rx5day': ' Annual maximum consecutive 5-day precipitation',
        'SDII': 'Ratio of annual total precipitation to number of wet days (≥1 mm)',
        'SU': 'Annual count when the max temperature >25 C',
        'TNn': 'Min value of daily min temperature during period of interest',
        'TNx': 'Max value of daily min temperature during period of interest',
        'TR': 'Counting no. of times when daily min temperature > 20 C',
        'TX10p': 'xyz',
        'TX90p': 'xyz1',
        'TXn': ' the min value of daily max temperature (TX) during the period of interest',
        'TXx': 'the maximum value of daily maximum temperature',
        'n_maxdy': 'abc',
        'n_mindy': 'abcd',
        'n_rain': 'abcde',
        'FD': 'counting the number of days when the min temperature was less than 0°C',
        'DTR': 'Avg difference between the max and min temperature during the period of interest'
    }

    if selected_x is None:
        return "Select one column"

    # Get the description for the selected x-column
    description = x_column_descriptions.get(selected_x, "No description available")

    return description


# Run the app
if __name__ == '__main__':
    app.run_server(port=8505)
In [ ]:
# Define Dash app
app = dash.Dash(__name__)

# Define available x-axis variables
x_columns = ['DTR',  'FD', 'PRCPTOT',
       'R10mm', 'R20mm', 'R5mm', 'Rx1day', 'Rx5day', 'SDII', 'SU', 'TN10p',
       'TN90p', 'TNn', 'TNx', 'TR', 'TX10p', 'TX90p', 'TXn', 'TXx', 'n_maxdy',
       'n_mindy', 'n_rain']  # Add your actual column names here

# Define app layout
app.layout = html.Div([
    html.H1("Data Visualization"),

    # Dropdown for selecting x column
    html.Div([
        html.Label("Select Indices:"),
        dcc.Dropdown(
            id='x-column-dropdown',
            options=[{'label': col, 'value': col} for col in x_columns],
            value=x_columns[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting month
    html.Div([
        html.Label("Select Month:"),
        dcc.Dropdown(
            id='month-dropdown',
            options=[{'label': str(month), 'value': month} for month in df_month_location['month'].unique()],
            value=df_month_location['month'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting station_id
    html.Div([
        html.Label("Select Station ID:"),
        dcc.Dropdown(
            id='station-dropdown',
            options=[{'label': station, 'value': station} for station in df_month_location['station_id'].unique()],
            value=df_month_location['station_id'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
    
    # Dropdown for selecting season
    html.Div([
        html.Label("Select Season:"),
        dcc.Dropdown(
            id='season-dropdown',
            options=[{'label': season, 'value': season} for season in df_season_location['season'].unique()],
            value=df_season_location['season'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),
  
    #radio button for year
    dcc.RadioItems(
    id='yearly-radio',
    options=[{'label': year, 'value': year} for year in df_year_location['year'].unique()],
    value=df_year_location['year'].unique()[0]
),
    
    
    # Line graph
    dcc.Graph(id='line-plot'),
    
    # Description of selected x-column
    html.Div(id='x-column-description', style={'margin-top': '20px'})
])

# Callback to update line plot based on dropdown selections

@app.callback(
    Output('line-plot', 'figure'),
    [Input('x-column-dropdown', 'value'),
     Input('month-dropdown', 'value'),
     Input('station-dropdown', 'value'),
     Input('season-dropdown', 'value')]
)
def update_line_plot(selected_x, selected_month, selected_station, selected_season):
    if selected_month is None:
        # If no month is selected, use the entire df_month_location dataset
        df_filtered = df_month_location
        title = f"{selected_x} Analysis (All Months)"
    else:
        # Filter df_month_location based on selected month
        df_filtered = df_month_location[df_month_location['month'] == selected_month]
        title = f"{selected_x} Analysis (Month {selected_month})"
    
    if selected_station is not None:
        # Filter by selected station
        df_filtered = df_filtered[df_filtered['station_id'] == selected_station]
        title += f" - Station {selected_station}"
    
    if selected_season is not None:
        # Filter df_season_location based on selected season
        df_season_filtered = df_season_location[df_season_location['season'] == selected_season]
        # Merge df_filtered with df_season_filtered on station_id and year
        df_filtered = pd.merge(df_filtered, df_season_filtered[['station_id', 'year']], on=['station_id', 'year'])
        title += f" - Season {selected_season}"
    
    # Calculate mean for selected x column grouped by year
    avg_values = df_filtered.groupby('year')[selected_x].mean().reset_index()
    
    # Create trace for line plot
    trace = go.Scatter(
        x=avg_values['year'],
        y=avg_values[selected_x],
        mode='lines+markers',
        name=selected_x
    )
    
    # Layout for line plot
    layout = go.Layout(
        title=title,
        xaxis={'title': 'Year'},
        yaxis={'title': selected_x}
    )
    
    return {'data': [trace], 'layout': layout}


# Callback to update description of selected x-column
@app.callback(
    Output('x-column-description', 'children'),
    [Input('x-column-dropdown', 'value')]
)
def update_x_column_description(selected_indices):
    # Dictionary containing descriptions of x-columns
    x_column_descriptions = {
    'PRCPTOT': 'Annual total precipitation from days ≥1 mm',
    'R10mm': ' Annual count when precipitation ≥10 mm',
    'R20mm': 'Annual count when precipitation ≥20 mm',
    'R5mm': 'Annual count when precipitation ≥5 mm',
    'Rx1day':'Annual maximum 1-day precipitation',
    'Rx5day':' Annual maximum consecutive 5-day precipitation',
    'SDII':'Ratio of annual total precipitation to number of wet days (≥1 mm)',
    'SU':'Annual count when the max temperature >25 C',
    'TNn':'Min value of daily min temperature during period of intrest',
    'TNx':'Max value of daily min temperature during period of intrest',
    'TR':'Counting no. of times when daily min temperature > 20 C',
    'TX10p':'xyz',
    'TX90p':'xyz1',
    'TXn':' the min value of daily maxtemperature (TX) during he period of interes',
    'TXx':'the maximum value of daily maximum temperature',
    'n_maxdy': 'abc',
    'n_mindy':'abcd',
    'n_rain':'abcde',
    'FD':'counting the number of days when the min emperature was less than 0°C',
    'DTR':'Avg difference between the max and min temperature during period of intrest'
    
}
    
    # Get the description for the selected x-column
    description = x_column_descriptions.get(selected_indices, "No description available")
    
    return description

# Run the app
if __name__ == '__main__':
    app.run_server(port=8501)

radio button for yearly working fine¶

In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
# Define Dash app
app = dash.Dash(__name__)

# Define available x-axis variables
x_columns = ['DTR',  'FD', 'PRCPTOT',
       'R10mm', 'R20mm', 'R5mm', 'Rx1day', 'Rx5day', 'SDII', 'SU', 'TN10p',
       'TN90p', 'TNn', 'TNx', 'TR', 'TX10p', 'TX90p', 'TXn', 'TXx', 'n_maxdy',
       'n_mindy', 'n_rain']  # Add your actual column names here

# Define app layout
app.layout = html.Div([
    html.H1("Data Visualization"),

    # Dropdown for selecting x column
    html.Div([
        html.Label("Select Indices:"),
        dcc.Dropdown(
            id='x-column-dropdown',
            options=[{'label': col, 'value': col} for col in x_columns],
            value=x_columns[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Radio button for selecting monthly or yearly data
    html.Div([
        html.Label("Select Data Type:"),
        dcc.RadioItems(
            id='data-type-radio',
            options=[
                {'label': 'Monthly', 'value': 'monthly'},
                {'label': 'Yearly', 'value': 'yearly'}
            ],
            value='yearly',
            labelStyle={'display': 'inline-block'}
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Radio button for selecting month (conditionally shown)
    html.Div([
        html.Label("Select Month:"),
        dcc.RadioItems(
            id='month-radio',
            options=[],  # Placeholder for dynamically populated options
            value='',  # Placeholder for the selected value
            labelStyle={'display': 'inline-block'}
        ),
    ], id='month-radio-container', style={'width': '48%', 'display': 'none'}),  # Initially hidden

    # Dropdown for selecting station_id
    html.Div([
        html.Label("Select Station ID:"),
        dcc.Dropdown(
            id='station-dropdown',
            options=[{'label': station, 'value': station} for station in df_month_location['station_id'].unique()],
            value=df_month_location['station_id'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Dropdown for selecting season
    html.Div([
        html.Label("Select Season:"),
        dcc.Dropdown(
            id='season-dropdown',
            options=[{'label': season, 'value': season} for season in df_season_location['season'].unique()],
            value=df_season_location['season'].unique()[0]
        ),
    ], style={'width': '48%', 'display': 'inline-block'}),

    # Line graph
    dcc.Graph(id='line-plot'),

    # Description of selected x-column
    html.Div(id='x-column-description', style={'margin-top': '20px'})
])

# Callback to dynamically populate the month radio button options based on the selected data type
@app.callback(
    Output('month-radio', 'options'),
    [Input('data-type-radio', 'value')]
)
def update_month_options(selected_type):
    if selected_type == 'monthly':
        # Fetch unique months from df_month_location DataFrame
        months = df_month_location['month'].unique()
        # Create options for the radio button
        month_options = [{'label': str(month), 'value': month} for month in months]
        return month_options
    else:
        return []

# Callback to dynamically show/hide the month radio button based on the selected data type
@app.callback(
    Output('month-radio-container', 'style'),
    [Input('data-type-radio', 'value')]
)
def toggle_month_radio(selected_type):
    if selected_type == 'monthly':
        return {'width': '48%', 'display': 'inline-block'}
    else:
        return {'display': 'none'}

# Callback to update line plot based on dropdown selections
@app.callback(
    Output('line-plot', 'figure'),
    [Input('x-column-dropdown', 'value'),
     Input('data-type-radio', 'value'),
     Input('month-radio', 'value')]
)
def update_line_plot(selected_x, selected_type, selected_month):
    # Your logic to update the plot based on the selected data type and month
    pass

# Callback to update description of selected x-column
@app.callback(
    Output('x-column-description', 'children'),
    [Input('x-column-dropdown', 'value')]
)
def update_x_column_description(selected_indices):
    # Dictionary containing descriptions of x-columns
    x_column_descriptions = {
        'PRCPTOT': 'Annual total precipitation from days ≥1 mm',
        'R10mm': 'Annual count when precipitation ≥10 mm',
        'R20mm': 'Annual count when precipitation ≥20 mm',
        'R5mm': 'Annual count when precipitation ≥5 mm',
        'Rx1day':'Annual maximum 1-day precipitation',
        'Rx5day':'Annual maximum consecutive 5-day precipitation',
        'SDII':'Ratio of annual total precipitation to number of wet days (≥1 mm)',
        'SU':'Annual count when the max temperature >25 C',
        'TNn':'Min value of daily min temperature during period of interest',
        'TNx':'Max value of daily min temperature during period of interest',
        'TR':'Counting no. of times when daily min temperature > 20 C',
        'TX10p':'xyz',
        'TX90p':'xyz1',
        'TXn':'the min value of daily max temperature (TX) during the period of interest',
        'TXx':'the maximum value of daily maximum temperature',
        'n_maxdy': 'abc',
        'n_mindy':'abcd',
        'n_rain':'abcde',
        'FD':'counting the number of days when the min temperature was less than 0°C',
        'DTR':'Avg difference between the max and min temperature during period of interest'
    }
    
    # Get the description for the selected x-column
    description = x_column_descriptions.get(selected_indices, "No description available")
    
    return description

# Run the app
if __name__ == '__main__':
    app.run_server(port=8501, debug=True)
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
# List of available x columns
x_columns = ['DTR (Diurnal Temperature Range)', 'FD (Frost Days)', 'PRCPTOT (Total Precipitation)',
             'R10mm (Days with ≥ 10mm rain)', 'R20mm (Days with ≥ 20mm rain)', 'R5mm (Days with ≥ 5mm rain)',
             'Rx1day (Wettest day)', 'Rx5day (Wettest 5-Day Period)', 'SDII (Average Daily Rainfall Intensity)',
             'SU (Summer Days)', 'TN10p (Cold Nights)', 'TN90p (Warm Nights)', 'TNn (Coldest Night)',
             'TNx (Warmest Night)', 'TR (Tropical Nights)', 'TX10p (Cold Days)', 'TX90p (Warm Days)',
             'TXn (Coldest Day)', 'TXx (Warmest Day)', 'n_maxdy', 'n_mindy', 'n_rain']

# Create Dash app
app = dash.Dash(__name__)

# Define app layout
app.layout = html.Div([
    html.H1("Data Visualization"),

    # Radio buttons for selecting data source
    html.Div([
        html.Label("Select Data Source:"),
        dcc.RadioItems(
            id='data-source-radio',
            options=[
                {'label': 'Year', 'value': 'year'},
                {'label': 'Season', 'value': 'season'},
                {'label': 'Month', 'value': 'month'}
            ],
            value='year',
            labelStyle={'display': 'inline-block'}
        ),
    ]),

    # Dropdown for selecting station
    html.Div([
        html.Label("Select Station:"),
        dcc.Dropdown(
            id='station-dropdown',
            options=[{'label': str(station), 'value': station} for station in df_year_location['station_id'].unique()],
            multi=True
        ),
    ]),

    # Dropdown for selecting x column
    html.Div([
        html.Label("Select X Column:"),
        dcc.Dropdown(
            id='x-column-dropdown',
            options=[{'label': col, 'value': col} for col in x_columns],
            value=x_columns[0]
        ),
    ]),

    # Line graph
    dcc.Graph(id='line-plot'),
])

# Callback to update line plot based on dropdown selections
@app.callback(
    Output('line-plot', 'figure'),
    [Input('data-source-radio', 'value'),
     Input('station-dropdown', 'value'),
     Input('x-column-dropdown', 'value')]
)
def update_line_plot(data_source, station_id, selected_x):
    if data_source == 'year':
        df = df_year_location.copy()
    elif data_source == 'season':
        df = df_season_location.copy()
    elif data_source == 'month':
        df = df_month_location.copy()
    else:
        df = pd.DataFrame()

    if station_id:
        df = df[df['station_id'].isin(station_id)]

    if not df.empty:
        # Calculate mean for selected x column grouped by year
        avg_values = df.groupby('year')[selected_x].mean().reset_index()

        # Create trace for line plot
        trace = go.Scatter(
            x=avg_values['year'],
            y=avg_values[selected_x],
            mode='lines+markers',
            name=selected_x
        )

        # Layout for line plot
        layout = go.Layout(
            title=f"{selected_x} Analysis",
            xaxis={'title': 'Year'},
            yaxis={'title': selected_x}
        )

        return {'data': [trace], 'layout': layout}
    else:
        return {}


# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)